/**
 * Created by LeungZ on 2016/10/31.
 */
class AppSrv {
    constructor(commonSrv) {
        "ngInject";
        this._commonSrv = commonSrv;
    }

    // --------------------------------------构建------------------------------------
    build(para) {
        return this._commonSrv('post', '/ctg-imageconsole-web/build/buildImage', para);
    }

    upload(para) {
        var fd = new FormData();
        fd.append('index', para.index);
        fd.append('total', para.total);
        fd.append('fileName', para.fileName);
        fd.append('data', para.data);
        fd.append('timestampDirectory', para.timestamp);

        return this._commonSrv('formData', '/ctg-imageconsole-web/build/uploadFile', fd);
    }

    getBuildList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/build/queryBuildList', {
            pageNum: page.currentPage,
            pageSize: 10,
            imageName: filter && filter.name,
            tag: filter && filter.ver,
            status: filter && filter.status
        });
    }

    getBuildLog(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/build/queryBuildLog', {
            dockerMicroImageId: para.id,
            lastTimeFileSize: para.size
        })
    }

    getBuildImages(obj) {
        return this._commonSrv('get', '/ctg-imageconsole-web/image/repositories', {
            pageNum: 1,
            pageSize: 999,
            sysUserCode:"sss",
            imageType:obj?obj:'002'
        })
    }
    deleteBuildImages(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/build/delete', {
           id
        })
    }

    //--------------------------------------发布------------------------------------
    getAppList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/list', {
            pageNum: page.currentPage,
            pageSize: page.page || 10,
            appCode: filter && filter.name
        })
    }

    deleteApp(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/delete', {
            appId: id
        })
    }

    checkAppCode(appCode,namespaceCode) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/appCodeValidation', {
            appCode: appCode,
            namespace:namespaceCode
        })
    }

    checkServiceCode(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/serviceCodeValidation', {
            appId: para.appId,
            serviceCode: para.serviceCode
        })
    }

    getYaml(data) {
        console.log(data)
        var para = [];
        angular.forEach(data.services, (service)=> {
            para.push({
                clusterCode:data.cluster.clusterCode,
                cpuLimit: service.podCpu,
                // memoryLimit: service.podMem && service.podMem + this._memUnit,
                memoryLimit: service.podMem,
                imageName:service.appImg.imageName,
                tag:service.appImg.ver.tag,
                namespace:data.namespaceCode,//租户编码
                // imageId: service.appImg.id,
                // imageTagId: service.appImg.ver.id,
                serviceCode: service.podAppName,
                servicePort:service.servicePort,
                groupCodeList: service.groups,
                replicas: service.podNum,
                cpuRequest: this._cpuReq,
                memoryRequest: this._memReq + this._memUnit,
                volumeList: service.volumes,
                volumeLogList: service.logVols,
                envs: service.envs
            })
        });
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/fixedJsonList', para)
    }

    releaseApp(para) {
        var paraData = {
            app: {
                appId: para.app.id,
                appCode: para.app.code,
                appName: para.app.name,
                description: para.app.desc,
                namespace:para.namespace
            },
            services: []
        };
        angular.forEach(para.services, (ser, index)=> {
            paraData.services.push(angular.extend({
                serviceId: ser.serviceId,
                serviceCode: ser.podAppName,
                url: ser.podDomain,
                externalPort: ser.podPort,
                k8sCode: para.clusterCode,
                namespace:para.namespace,//租户编码
                replicas: ser.podNum,
                yamlContent: para.yamls[index]
            }, ser.atsCheck ? {
                isAutoscalable: 1,
                maxPodNum: ser.maxPodNum,
                minPodNum: ser.minPodNum,
                elasticIndicator: ser.atsTarget,
                elasticStartTime: ser.atsStartTime,
                elasticEndTime: ser.atsEndTime,
                timeRate: ser.atsSpace,
                maxCpu: ser.atsLimit
            } : {
                isAutoscalable: 0
            }));
        });

        if (para.services.length == 1 && para.services[0].serviceId) {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/replace', paraData.services[0]);
        }

        if (para.app.id) {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/create', {
                app: paraData.app,
                service: paraData.services[0]
            });
        }

        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/create', paraData);
    }

    checkInvalid(forms) {
        var invalid = false;
        angular.forEach(Object.keys(forms), key => {
            forms[key].$submitted = true;
            if (forms[key].$invalid) invalid = true;
        });

        return invalid;
    }

    getAppDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/detail', {
            appId: id
        })
    }

    update(para) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/updateDescription', {
            appId: para.id,
            description: para.content
        })
    }

    getConfig() {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/configs')
    }

    rollback(para){
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/rollback',{
            serviceId: para.serviceId,
            versionId: para.version
        });
    }

    //------------------------------服务------------------------------------

    getServiceOfApp(para) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/list', {
            appId: para.id,
            pageNum: para.currentPage,
            serviceId: para.serviceId,
            pageSize: 5
        })
    }

    getServiceDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/dms/app/service/detail', {
            serviceId: id
        })
    }

    deleteService(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/delete', {
            serviceId: id
        })
    }

    stopService(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/terminate', {
            serviceId: id
        })
    }

    startService(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/start', {
            serviceId: id
        })
    }

    advUpdate(para, serId) {
        if (para.currentForm == 'domain') {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/configLvs', {
                url: para.podDomain,
                externalPort: para.podPort,
                serviceId: serId
            })
        }
        if (para.currentForm == 'ats') {
            return this._commonSrv('post', '/ctg-imageconsole-web/dms/app/service/configAutoScaler', para.atsCheck ? {
                isAutoscalable: 1,
                maxPodNum: para.maxPodNum,
                minPodNum: para.minPodNum,
                elasticIndicator: para.atsTarget,
                elasticStartTime: para.atsStartTime,
                elasticEndTime: para.atsEndTime,
                timeRate: para.atsSpace,
                maxCpu: para.atsLimit,
                serviceId: serId
            } : {
                isAutoscalable: 0,
                serviceId: serId
            });
        }
    }

    //--------------------------------------服务依赖------------------------------------

    getSerDepList(page, filter) {
        return this._commonSrv('get', '/ctg-imageconsole-web/serviceDependence/queryList', {
            pageNum: page.currentPage,
            pageSize: 10,
            name: filter && filter.name
        });
    }

    getSerDepDetail(id) {
        return this._commonSrv('get', '/ctg-imageconsole-web/serviceDependence/queryDetail', {
            id: id
        })
    }

    saveSerDep(id, name, descJson) {
        return this._commonSrv('post', '/ctg-imageconsole-web/serviceDependence/save', {
            id: id,
            name: name,
            descJson: descJson

        })
    }

    deleteSer(id) {
        return this._commonSrv('post', '/ctg-imageconsole-web/serviceDependence/delete', {
            id: id
        })
    }
}
export default AppSrv